package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.SearchRaceResponse
import com.gexiaobao.pigeon.databinding.ItemSearchRaceListBinding


/**
 * @Author      : hxw
 * @Date        : 2023/8/4 14:20
 * @Describe    :
 */
class SearchRaceListAdapter(data: ArrayList<SearchRaceResponse.ListBean>) :
    BaseQuickAdapter<SearchRaceResponse.ListBean, DataBindBaseViewHolder>(R.layout.item_search_race_list, data) {

    private var isExpand: Boolean = true
    private var expandPosition: Int = 0

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: SearchRaceResponse.ListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemSearchRaceListBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val openTime = item.openTime
        if (openTime.isNotEmpty() && openTime.contains("：")) {
            holder.setText(R.id.openTime, if (openTime.split("：").size > 1) openTime.split("：")[1] else "")
        } else {
            holder.setText(R.id.openTime, openTime)
        }
        val flyAddr = item.flyAddr
        if (flyAddr.isNotEmpty() && flyAddr.contains("：")) {
            holder.setText(R.id.flyAddr, if (flyAddr.split("：").size > 1) flyAddr.split("：")[1] else "")
        } else {
            holder.setText(R.id.flyAddr, flyAddr)
        }

        val tvRaceName = holder.getView<AppCompatTextView>(R.id.tvRaceName)
        val constraintLayoutPigeonDetailInfo = holder.getView<ConstraintLayout>(R.id.constraintLayoutPigeonDetailInfo)
        constraintLayoutPigeonDetailInfo.visibility = if (holder.layoutPosition == expandPosition) View.VISIBLE else View.GONE
        tvRaceName.setOnClickListener {
            togglePosition(holder.layoutPosition)
        }
    }

    private fun togglePosition(position: Int) {
        expandPosition = if (expandPosition !=position) {
            notifyItemChanged(expandPosition)
            position
        } else {
            -1
        }
        notifyItemChanged(position)
    }
}